package com.cincc.general;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import pub.devrel.easypermissions.EasyPermissions;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.cincc.general.common.AttrsConstant;
import com.cincc.general.common.SharePreferenceUtil;

import java.util.List;

/**
 * 主页面
 *
 * @author fengwawa
 */
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

    EditText etNumber;

    /**
     * APP需要的权限
     */
    String[] mPermissionList = new String[]{Manifest.permission.RECORD_AUDIO,
            Manifest.permission.MODIFY_AUDIO_SETTINGS,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE};
    public static int CALL_PHONE_REQUEST_CODE = 0x1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkNeededPermission();
        initView();
    }

    /**
     * 初始化实体组件
     */
    private void initView() {
        etNumber = findViewById(R.id.et_number);
    }


    /**
     * 权限校验
     */
    public void checkNeededPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (EasyPermissions.hasPermissions(this, mPermissionList)) {
                //已经同意过
            } else {
                //未同意过,或者说是拒绝了，再次申请权限
                EasyPermissions.requestPermissions(
                        //上下文
                        this,
                        //提示文字
                        "需要以下权限才能正常使用通话功能",
                        //请求码
                        CALL_PHONE_REQUEST_CODE,
                        //权限列表
                        mPermissionList
                );
            }
        }
    }


    public void makeCall(View view) {

        String callNumber = etNumber.getText().toString().trim();

        if (TextUtils.isEmpty(callNumber)) {
            Toast.makeText(this, "呼叫号码不能为空!", Toast.LENGTH_SHORT).show();
        } else {
            String xToken = (String) SharePreferenceUtil.getInstance().getValue(AttrsConstant.X_TOKEN, "");
            if (TextUtils.isEmpty(xToken)) {
                Toast.makeText(this, "请先填写配置项", Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(this, CallActivity.class);
                intent.putExtra("callNumber", callNumber);
                startActivity(intent);
            }
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull @org.jetbrains.annotations.NotNull List<String> perms) {
        for (String s : perms) {
            Log.d("MainActivity", "授权的权限：" + s);
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull @org.jetbrains.annotations.NotNull List<String> perms) {
        for (String s : perms) {
            Log.d("MainActivity", "拒绝的权限：" + s);
        }
    }

    public void Setting(View view) {
        Intent i = new Intent(this, SettingActivity.class);
        startActivity(i);
    }
}